/*
 添加预约时自动显示当前用户名和电话
 */

let apmt_title=new Vue({
    el:"form",
    data:{
        name:"123",
        phone:"321",
        age:"25",
        departments:[],
        appointtime:"2021-06-15T18:30",
        departmentId:"",
        doctorId:"",
        description:"",
        sex:"",
        userId:""

    },
    methods : {
        //加载基本信息及选项
        loadInformation: function () {
            //发送请求获取当前用户信息
            axios({
                url: "/add_appointment",
                method: "GET",
            }).then(function (r) {
                //初始化相应的可免填信息
                apmt_title.name = r.data.name;
                apmt_title.phone = r.data.phone;
                apmt_title.userId = r.data.id;
                console.log("当前用户性别"+r.data.gender)
                var gender=r.data.gender;
                $("input[name='sex']").removeAttr("checked");
                $("input[value="+gender+"] ").attr("checked","checked")

            })
            //加载预约科室的多选项
            axios({
                url : "/add_appointment/department",
                method:"get",
            }).then(function(r) {
                apmt_title.departments=r.data;
                console.log(apmt_title.departments)
            })
            //加载当前时间,
            this.getDate();
            //加载当前用户性别


        },
        //上传预约信息
        submitAppointment:function() {
            let form=new FormData();
            //从各相关元素中获取用户填入的信息并填入form表单
            this.name=$("#name").val();
            form.append("name",this.name);
            this.phone=$("#phone").val();
            form.append("phone",this.phone);
            this.age=$("#age").val();
            form.append("age", this.age);
            form.append("userId", this.userId);
            this.departmentId=$("#selectDepartment option:selected").attr("id");
            console.log("this.departmentId:"+this.departmentId)
            form.append("officeId",this.departmentId )
            this.doctorId=$("#selectDoctor option:selected").val();
            form.append("doctorId",this.doctorId )
            this.appointtime=$("#date").val();
            form.append("appointtime",this.appointtime)
            this.description=$("textarea").val();
            form.append("description",this.description)
            let selectGender= document.getElementsByName("sex");
            //判断性别选项
            for (let i = 0; i < selectGender.length; i++) {
                if (selectGender.item(i).checked) {
                    this.sex= selectGender.item(i).getAttribute("value")
                }
            }
            form.append("sex",this.sex )
            console.log(form);
            //异步上传预约信息
            axios({
                url:"/add_appointment",
                method:"POST",
                data: form
            }).then(function (r) {
                console.log("传送成功")
                if(r.data!="预约成功"){
                    alert(r.data)
                    return;
                }
            alert("预约成功")
                location.reload();
            })
        },
        //重新填写信息
        clearAll:function () {
            console.log("清空所有" )
            //清空所有已填信息
            for(let i=0;i<2;i++){
                $(".info").val("");
            }
            //清除查询出的医生选项
            $("option[name='id']").remove();
            //重新获取当前时间
            this.getDate();
        },
        //获取当前时间
        getDate:function() {
            var today = new Date();
            var yyyy = today.getFullYear();//获取当前年,月,日,时,分
            var MM = today.getMonth() + 1;
            var dd = today.getDate();
            var hh = today.getHours();
            var mm = today.getMinutes();
            //如果月,日,时,分为个位数,就在前面加个0
            if (MM < 10) {
                MM = "0" + MM;
            }
            if(dd<10){
                dd="0"+dd;
            }
            if(hh<10){
                hh="0"+hh;
            }
            if(mm<10){
                mm="0"+mm;
            }
            //最终得到的时间格式
            this.appointtime = yyyy + "-" + MM + "-" + dd + "T" + hh + ":" + mm;
            //返回给对应的元素
            // let dateControl = document.getElementById("date");
            // dateControl.value = this.appointtime;
        }

    },
    created: function (){
            this.loadInformation();
        }

})
//得到部门标签后加载部门医生
function  loadDoctors(officeId){
    console.log("部门ID:"+officeId);
    if(officeId==0) {
        $("#selectDoctor option").remove();
        $("#selectDoctor").append("<option value=''>请选择--</option>")
    }else{
        axios({
            url:"/add_appointment/loadDoctors",
            method:"GET",
            //根据科室id获取医生
            params:{officeId:officeId}
        }).then(function(r){
            //清空当前的所有医生选项,因为之前有可能加载过其他部门的医生
            $("#selectDoctor option").remove();
            console.log(r.data);
            //获取当前选中科室的医生
            let doctors=r.data;
            var id;
            var name;
            $("#selectDoctor").append("<option value=''>请选择--</option>")
            //生成每个医生的选项,放进了医生id和医生姓名
            for(let i=0;i<doctors.length;i++){
                id=doctors[i].id;
                name=doctors[i].name;
                $("#selectDoctor").append("<option value="+id+">"+name+"</option>")
            }
        })
    }

}

